﻿
var APP=is_app();
var app_url = location.origin;
 var RATE = Number($.cookie("ParcelRate"));
 var TEAM=0;
 var PlaceOrder=false;//代下单
(function () {
    var parcels = {
        init: function () {       	
        	args = GetUrlParms();				
			if(args["team"]!=undefined){						
				 TEAM = args["team"];//产品ID					
			} 
			if(args["cid"]!=undefined){						
				 Customer_id = args["cid"];	
				 PlaceOrder=true;//代下单
			} 
            this.bindEvents();
            this.loadParcels();
        },
        bindEvents: function () { 
            var self = this;
             //筛选
            $("#term_app_btn").on("click",function(){
            	if(PlaceOrder==true){
            		location.href="/Customer/ParcelFilter?team=3&cid="+Customer_id;
            	}else{
            		location.href="/Customer/ParcelFilter?team="+TEAM;
            	}
            });
             //app
           $("#parcels").on("click","#app_link_item",function(){
           		var link_url=$(this).attr("data-src");
           		if(APP){
           			var appJson={};
           			appJson.title=_("统包详情");
           			appJson.url=app_url+link_url;	
           			YiYi_APP.showNewPage(JSON.stringify(appJson));
           		}else{
           			location.href=link_url;
           		}       
           });
           //点击菜单
			$("#show_menu_btn").on("click",function(){
				$("#assistant_menu_box").toggle();
			});
			//关闭菜单
			$("#assistant_menu_box").on("click",function(){
				$("#assistant_menu_box").hide();
			});
			//新增
			$("#add_btn").on("click",function(){
				var link_url="/Team/ParcelManagement?type=2";
				if(APP){
					var appJson={};
           			appJson.title=_("统包新增");
           			appJson.url=app_url+link_url;	
           			YiYi_APP.showNewPage(JSON.stringify(appJson));
				}else{
					location.href=link_url;
				}
			});
           
        },
        //加载数据
        loadParcels: function (first) {
            myApp.showIndicator();//显示加载中
        	var self = this;
        	var params=PARAMS;
         	if(params==null){
         	 	params={};
         	 }
         	if(TEAM==2){
         		params.IsMyStock=true;//现货
         	}else{
         		params.OnShelves = "1"; 
         	}
         	// if(TEAM==0){
           	//  	params.CustomerID = CustomerID;//客户ID
           	// }
            params.useDict = true;
			params.limit=500;
			params.GoodsType=2;
            var params = JSON.stringify(params);
            var url ="/Team/GetProductData";
            $.ajax({
                url: url,
                data: params,
                type: "POST",
                contentType: "application/json; charset=utf8",
                success: function (result) {
                    tempSupplier = 0;
                    var res=eval('(' + result + ')')
                    if (res.msgcode == true) {
                        window.GOODS_ITEMS = res.rows;
                        var dtRenderStart = new Date().getTime();
                        isSearching = false;

                        if (res.msginfo.rows.length == 0) {
                            $("#parcels").html('<div class="no_data_tip">'+_("没有符合条件的结果")+'</div>');
                        } else {
                            var parcels = [];
                           
                            for (var i = 0; i < res.msginfo.rows.length; i++) {
                                var row = res.msginfo.rows[i];
                                parcels.push(
                                    '<li data="', row.Supplier, '" itemtype="" itemid="', row.ID, '">'
								)
								if(TEAM==1 || TEAM ==2){
									parcels.push(
										'<a href="javascript:void(0)" data-src=',"/","Team/ParcelDetails/?ID=",row.ID,"&team=",TEAM,PlaceOrder?"&cid="+Customer_id:"",' id="app_link_item" data-type="'+GetCategory(row.Category)+'">'
									)
								}else if(TEAM==3){
                                    parcels.push(
										'<a href="javascript:void(0)" data-src=',"/","Team/ParcelDetails/?ID=",row.ID,"&team=",TEAM,PlaceOrder?"&cid="+Customer_id:"", "&userName=",getUrlParam('userName'),' id="app_link_item" data-type="'+GetCategory(row.Category)+'">'
									)
								}else{
                                    parcels.push(
                                        '<a href="javascript:void(0)" data-src=',"/","Customer/ParcelDetails/?ID=",row.ID,"&team=",TEAM,PlaceOrder?"&cid="+Customer_id:"",' id="app_link_item" data-type="'+GetCategory(row.Category)+'">'
                                    )
								}
                                parcels.push(
	                                           '<div  class="image_app_box">',
	                                        	'<img class="product_img lazy" data-src="', imgSrc(row.Images), '" src="'+imgSrc(row.Images)+'" alt="统包"/>',
	                                        '</div>',
                                        	'<div class="product_data">',
		                               			'<h4>',
			                            			'<span class="CurrentRMB">￥<b title="价格">',returnRMBPrice(row),'</b></span>',
			                            			returnPrice(row),
		                            			'</h4>',
		                              			'<div class="data_box">',
		                               				'<div>',
		                               					'<b title="类型">',GetCategory("parcels",row.Category),'</b>',
		                               					'<b title="形状">', GetText("parcels", row.Category, row.Shape, "Shape"), '</b>',
		                               					'<b title="重量">',row.Size,'</b>',
		                               				'</div><div>',
		                               					'<b title="颜色">', GetText("parcels", row.Category, row.Color, "Color"), '</b>',
		                               					'<b title="净度">',row.Clarity,'</b>',
		                               				'</div>',
		                               				'<div>',
//		                               					'<b title="供应商">',ConvertSupplier(row),'</b>',
		                               					'<b title="所在地">', GetText("parcels", row.Category, row.Place, "Place"), '</b>',
		                               				'</div>',
		                            			'</div>',
		                            		'</div>',
		                            	'</a>',
		                            '</li>'
                                     );
                            }



                           $("#parcels").html(parcels.join(''));
                           
                            //懒加载
                             var n = 0,
						     imgNum = $("#parcels li").length,
						     img = $('#parcels li .product_img');
						   // myApp.initImagesLazyLoad(img)
						    $$('img.lazy').trigger('lazy');
                          //图片加载出来时居中显示 放在 framework.js
                            $(".image_app_box .product_img").load(function(){
                                //图片加载出来时居中显示  产品图  统包
                                var w=$(this).width();
                                var h=$(this).height();
                                var docu_w=$(window).width();
                                if(docu_w>760){
                                    var screenW=244;
                                }else{
                                    var screenW=docu_w/2;
                                }
                                if(w>h){
                                    var w_scale=w/screenW;
                                    var now_h=h/w_scale;
                                    var pa_top=screenW/2-now_h/2;
                                    $(this).css({
                                        "width":"100%",
                                        "padding-top":pa_top+"px",
                                        "padding-bottom":pa_top+"px"
                                    });
                                }else if(h>w){
                                    var h_scale=h/screenW;
                                    var now_w=w/h_scale;
                                    var pa_left=screenW/2-now_w/2;
                                    $(this).css({
                                        "height":"100%",
                                        "padding-left":pa_left+"px",
                                        "padding-right":pa_left+"px"
                                    });
                                }else{
                                    $(this).css({
                                        "height":"100%",
                                        "width":"100%"
                                    });
                                }

                            });


		                     currentShow();  //币种设置
                        }

                        console.info(new Date().getTime() - dtRenderStart + "ms");
                    } else {
                        toast(res.msginfo);
                    }
                    myApp.hideIndicator();//隐藏加载中
                }
            });
        },

    };
    parcels.init();
})();
//没权限返回的价格
function returnPrice(data){
	//if(data.Price || data.Price==0){
		//var val=data.Weight?(((data.Price)/(data.Weight)).toFixed(2)):0;
		var priceHtml= ['<span class="CurrentUSD">$<b title="单价">'+formatUSDPrice(data.Price)+'</b></span>'].join('');
		return priceHtml;		
	//}else{
	//	return '<span style="font-size:14px;">$<b title="价格">权限不足</b></span>';	
	//}
}
function returnRMBPrice(data){
	if(data.Price || data.Price==0){
		//return data.Weight?((((data.Price)/(data.Weight)))*RATE).toFixed(1):0;
		return formatRMBPrice(data.Price*RATE);
	}else{
		return '-';	
	}
}
//优先显示简称
function ConvertSupplier(data) {
//	if(data.Supplier != undefined) {
//		if(data.ShortName != "") {
//			return data.ShortName;
//		} else {
			return data.Supplier;
//		}
//	} else
//		return "-";
}
//返回图片链接
function imgSrc(images) {
    if (images) {
        images = images.split('|');
        var small = images[0];
        var big = small.replace('800x800', '1920x1920');
        return small;
    }
    return '../images/no_img.jpg';//无图时返回默认图片
}




//显示图片
function ShowImg(src) {
    if (src) {
        var big = src.replace('800x800', '1920x1920');
        return ['<a href="' + src + '"><img src="' + src + '" class="parcel_img" style="width:100%;"/></a>'].join('');
    }
    else
        return "";
}
